package com.cn.appcore.utils

import android.content.Context
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import org.json.JSONArray
import org.json.JSONObject
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader

/**
 * 项目：DesignerAjDgo
 * 包名：com.cn.designerajdgo
 * 类名：GsonUtil
 * 创建时间：2021/10/26 9:43 上午
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
object GsonUtil {

    fun isJsonString(jsonString: String): Boolean {
        return try {
            JSONObject(jsonString)
            true
        } catch (e: Exception) {
            try {
                JSONArray(jsonString)
                true
            } catch (e: Exception) {
                false
            }
        }
    }

    /**
     * map转为json
     *
     * @param maps
     * @return
     */
    fun mapToJsonStrObj(maps: HashMap<String, Any>): String? {
        val gson = Gson()
        return gson.toJson(maps)
    }
    /**
     * 将Json数据解析成相应的映射对象
     *
     * @param jsonData JSON
     * @param type     对象
     * @return
     */
    fun <T> gsonToJsonClass(jsonData: String?, type: Class<T>?): T {
        // Gson gson = new Gson();
        val gson = GsonBuilder().serializeNulls().create()
        return gson.fromJson(jsonData, type)
    }

    /**
     * 将Json数据解析成相应的映射对象
     *
     * @param jsonData JSON
     * @param type     对象
     * @return
     */
    fun <T> GsonToJsonClass(jsonData: String?, type: Class<T>?): T {
         var gson = Gson()
//        val gson = GsonBuilder().serializeNulls().create()
        return gson.fromJson(jsonData, type)
    }
    /**
     * 对象转json
     */
    fun<T> classToJsonStr(type: T): String? {
        val gson = Gson()
        return gson.toJson(type)
    }


    /**
     * 解析本地地址的json
     */
    fun getJson(context: Context, fileName: String?): String? {
        val stringBuilder = StringBuilder()
        try {
            val assetManager = context.assets
            val bf = BufferedReader(
                InputStreamReader(
                    assetManager.open(fileName!!)
                )
            )
            var line: String?
            while (bf.readLine().also { line = it } != null) {
                stringBuilder.append(line)
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return stringBuilder.toString()
    }

    /**
     * 解析本地地址的json
     */
//    fun parseData(result: String?): ArrayList<JsonBean>? { //Gson 解析
//        val detail: ArrayList<JsonBean> = ArrayList<JsonBean>()
//        try {
//            val data = JSONArray(result)
//            val gson = Gson()
//            for (i in 0 until data.length()) {
//                val entity: JsonBean =
//                    gson.fromJson(data.optJSONObject(i).toString(), JsonBean::class.java)
//                detail.add(entity)
//            }
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
//        return detail
//    }

    /**
     * map转为json
     *
     * @param maps
     * @return
     */
    fun mapToJsonStrObjEncryption(maps: HashMap<String, Any>): String? {
        val gson = Gson()

        return  DecryptUtils.encryptionData(gson.toJson(maps))
    }

    fun mapToJsonStr(maps: HashMap<String, Any>): String? {
        val gson = Gson()
        return  gson.toJson(maps)
    }
    /**
     * json转为map String,object
     *
     * @param jsonStr json字符串
     * @return map
     */
    fun jsonToMap(jsonStr: String?): HashMap<String?, Any?>? {
        var objMap: HashMap<String?, Any?>? = null
        val gson = Gson()
        if (gson != null) {
            val type = object : TypeToken<HashMap<String?, Any?>?>() {}.type
            objMap = gson.fromJson(jsonStr, type)
        }
        return objMap
    }



    fun jsonToList(jsonStr: String?): List<String> {
        val gson = Gson()
        val listType = object : TypeToken<List<String>>() {}.type
        val list: List<String> = gson.fromJson(jsonStr, listType)
        return  list

    }


    fun jsonToListKotlin(jsonStr: String?): MutableList<String> {
        val gson = Gson()
        val listType = object : TypeToken<List<String>>() {}.type
        val list: MutableList<String> = gson.fromJson(jsonStr, listType)
        return  list

    }

    //list转map
    fun listToMap(list: List<String>): HashMap<String, String> {
        return list.associate { it to it.toUpperCase() } as HashMap<String, String>

    }

    //Json  转list《String》
    fun strToList(str: String):  List<String> {
        val gson = Gson()
        val array = gson.fromJson(str, Array<String>::class.java)
        return array.toList()
    }


}